package cn.tedu.charging.role.service.impl;

import cn.tedu.charging.role.mapper.RoleMapper;
import cn.tedu.charging.common.pojo.param.RoleQuery;
import cn.tedu.charging.role.pojo.param.RoleSaveParam;
import cn.tedu.charging.role.pojo.po.Role;
import cn.tedu.charging.common.pojo.po.RoleStaff;
import cn.tedu.charging.common.pojo.vo.RoleVO;
import cn.tedu.charging.role.service.RoleService;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public List<RoleVO> selectRole(RoleQuery roleQuery) {
        log.debug("查询角色业务层的入参为{}",roleQuery);
        log.debug("要判断是否含有员工Id 入参为{}",roleQuery.getStaffId());
        if (roleQuery.getStaffId()==null){
            log.debug("查询条件中不含有员工id");
            log.debug("查询角色业务层调用mapper层的入参为{}",roleQuery);
            List<RoleVO> list = roleMapper.selectRole(roleQuery);
            log.debug("查询角色业务层调用mapper层的入参为{}出参为{}",roleQuery,list);
            return list;
        }else {
            log.debug("查询条件中含有员工ID");
            RoleStaff roleStaff = new RoleStaff();
            roleStaff.setStaffId(roleQuery.getStaffId());
            List<RoleStaff> roleStaffs = selectRoleStaff(roleStaff);
            List<RoleVO> list=new ArrayList<>();
            for (int i =0;i<roleStaffs.size();i++) {
//            获取角色id
                Long roleId = roleStaffs.get(i).getRoleId();
//            新建查询条件
                RoleQuery roleQuery1 = new RoleQuery();
//            给查询条件赋值权限id
                roleQuery1.setId(roleId);
//
                List<RoleVO> roleVOS = roleMapper.selectRole(roleQuery1);

                list.add(roleVOS.get(0));
            }
            return list;
        }

    }

    @Override
    public void saveRole(RoleSaveParam roleSaveParam) {
        log.debug("保存角色业务层的入参为{}",roleSaveParam);
//        判断是新增还是修改
        if(roleSaveParam.getId()==null){
            log.debug("新增角色业务层调用mapper层的入参为{}",roleSaveParam);
            Role role = new Role();
            BeanUtils.copyProperties(roleSaveParam,role);
            role.setCreateTime(new Date());
            roleMapper.insert(role);
            log.debug("新增角色业务层调用mapper层的入参为{}没有出参",roleSaveParam);
        }else{
            log.debug("修改角色业务层调用mapper层的入参为{}",roleSaveParam);
            Role role = new Role();
            BeanUtils.copyProperties(roleSaveParam,role);
            role.setUpdateTime(new Date());
            UpdateWrapper<Role> roleUpdateWrapper = new UpdateWrapper<>();
            roleUpdateWrapper.eq("id",roleSaveParam.getId());
            roleMapper.update(role,roleUpdateWrapper);
            log.debug("修改角色业务层调用mapper层的入参为{}没有出参",roleSaveParam);
        }
    }

    @Override
    public void deleteRole(Long roleId) {
        log.debug("删除角色业务层的入参为{}",roleId);
        roleMapper.deleteById(roleId);
        log.debug("成功删除角色业务层的入参为{}",roleId);
    }

    @Override
    public List<RoleStaff> selectRoleStaff(RoleStaff roleStaff) {
        log.debug("查询角色员工业务层的入参为{}",roleStaff);
        log.debug("查询角色员工业务层调用mapper层的入参为{}",roleStaff);
        List<RoleStaff> list =roleMapper.selectRoleStaff(roleStaff);
        log.debug("查询角色员工业务层调用mapper层的入参为{}出参为{}",roleStaff,list);
        return list;

    }

    @Override
    public void deleteRoleStaff(Long roleId, Long staffId) {
        log.debug("删除角色员工业务层的入参为{}{}",roleId,staffId);
        log.debug("删除角色员工业务层调用mapper层的入参为{}{}",roleId,staffId);
        RoleStaff roleStaff = new RoleStaff();
        roleStaff.setRoleId(roleId);
        roleStaff.setStaffId(staffId);
        roleMapper.deleteRoleStaff(roleStaff);
        log.debug("删除角色员工业务层调用mapper层的入参为{}{}没有出参",roleId,staffId);
    }

    @Override
    public void addRoleStaff(Long roleId, Long staffId) {
        log.debug("新增角色员工业务层的入参为{}{}",roleId,staffId);
        RoleStaff roleStaff = new RoleStaff();
        roleStaff.setRoleId(roleId);
        roleStaff.setStaffId(staffId);
        log.debug("新增角色员工业务层调用mapper层的入参{}",roleStaff);
        roleMapper.addRoleStaff(roleStaff);
        log.debug("新增角色员工业务层调用mapper层的入参{}没有出参",roleStaff);
    }

    @Override
    public void updateStatus(Long roleId, String status) {
        log.debug("修改角色状态业务层的入参为{}{}",roleId,status);
        UpdateWrapper<Role> roleUpdateWrapper = new UpdateWrapper<>();
        roleUpdateWrapper.eq("id",roleId);
        Role role = new Role();
        role.setStatus(status);
        log.debug("修改角色状态业务层调用mapper层的入参为{}{}",role,roleUpdateWrapper);
        roleMapper.update(role,roleUpdateWrapper);
        log.debug("修改角色状态业务层调用mapper层的入参为{}{}没有出参",role,roleUpdateWrapper);
    }

    @Override
    public List<RoleVO> selectUnBindPerm(Long staffId) {
        log.debug("查询未绑定角色业务层的入参为{}",staffId);
        log.debug("查询所有权限");
        List<RoleVO> roleVOS = selectRole(new RoleQuery());
        log.debug("查询所有角色的出参为{}",roleVOS);
        RoleQuery roleQuery = new RoleQuery();
        roleQuery.setStaffId(staffId);
        log.debug("获取此员工所绑定的角色的入参为{}",roleQuery);
        List<RoleVO> roleVOS1 = selectRole(roleQuery);
        log.debug("获取此角色所含有的权限的入参为{}出参为{}",roleQuery,roleVOS1);
        log.debug("获取此角色未绑定的权限集合的入参为{}{}",roleVOS,roleVOS1);
        List<RoleVO> unBindPermList = difference(roleVOS,roleVOS1);
        log.debug("获取此角色未绑定的权限集合的出参为{}",unBindPermList);
        return unBindPermList;
    }

    /**
     * 全部集合 与部分集合的查值集合
     * @param listAll
     * @param listByStaffId
     * @return
     */
    public static List<RoleVO> difference(List<RoleVO> listAll, List<RoleVO> listByStaffId) {
        log.debug("进入difference方法用来获取未绑定角色集合");
        // 创建一个新的 ArrayList 用于存储结果
        List<RoleVO> result = new ArrayList<>(listAll);

        // 将 listB 转换成一个 Set 用于快速查找
        Set<Long> idsInListB = new HashSet<>();
        for (RoleVO item : listByStaffId) {
            idsInListB.add(item.getId());
        }

        // 移除所有在 listB 中出现的元素
        result.removeIf(item -> idsInListB.contains(item.getId()));

        return result;
    }
}
